% ***********************************************************************************************
% m_scale.m 
% ***********************************************************************************************
clear all; 
close all;
format shortG;
% ==================== LOAD FUNDAMENTAL PARAMETERS ========================
load parameters.mat;                     % basic parameters loading
load calib_productivity_amenity.mat;     % inverted fundamentals every 5 years from 1955
load calib_setup.mat;                    % Info data every 5 years from 1945
load commuting_cost.mat;  
load calib_auxiliary.mat;
load fundamentals_1950.mat;              

Rdata=Rdata_all; Ldata=Ldata_all;  
totalpop=sum(Rdata,1);
N=size(ID,1);
NT=6; 

% ==================== AGGREGATE VALUE OF FLOOR SPACES ====================
h45=hsupply45;                          
h50=(1-psi).*h45+Hsupplydata(:,1); 
h55=(1-psi).*h50+Hsupplydata(:,2); 
h60=(1-psi).*h55+Hsupplydata(:,3);
h65=(1-psi).*h60+Hsupplydata(:,4);
h70=(1-psi).*h65+Hsupplydata(:,5);
h75=(1-psi).*h70+Hsupplydata(:,6); 
hstock=[h50,h55,h60,h65,h70,h75]; 
hvalue=sum(Qdata.*hstock,1); 

% === AGGREGATE EXPENDITURE ON FLOOR SPACES ==============================
aa=[a50, aa]; 
AA=aa.*(Ldata(:,2:end)./repmat(Area,1,NT)).^alphaest;
w=(AA.*Qdata.^(-gammaH)).^(1/gammaL); 
hexp=(mu+gammaH/gammaL).*sum(w.*Ldata(:,2:NT+1),1); 

% === SCALE OF FLOOR SPACE PRICES =======================================
qbar=(hexp./hvalue).^(1/(1+gammaH/gammaL)); 
Qadj=repmat(qbar,N,1).*Qdata; 
save('calib_qadjusted.mat', 'Qadj');

% === SCALE OF FLOOR SPACE SUPPLY +======================================
hbar=sum(Hsupplydata,1)./sum((Qadj.^eta).*repmat(Area,1,NT),1);
save('calib_hscale.mat', 'hbar', 'hstock');
